package pers.gxm.mediator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends Frame implements ActionListener , Mediator {
    public static void main(String[] args) {
        LoginFrame sample = new LoginFrame("Mediator Sample");
    }

    private ColleagueCheckbox checkboxGuest;
    private ColleagueCheckbox checkboxLogin;
    private ColleagueTextField textUser;
    private ColleagueTextField textPassword;
    private ColleagueButton buttonOK;
    private ColleagueButton buttonCancel;

    public LoginFrame(String title){
        super(title);
        setBackground(Color.lightGray);
        setLayout(new GridLayout(4,2));
        createColleagues();

        add(checkboxGuest);
        add(checkboxLogin);
        add(new Label("Username:"));
        add(textUser);
        add(new Label("Password:"));
        add(textPassword);
        add(buttonOK);
        add(buttonCancel);

        colleagueChanged();
//        下面这行可以注释看看效果
        pack();
        show();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("e = " + e);
        System.exit(0);
    }

    @Override
    public void createColleagues() {
        CheckboxGroup group = new CheckboxGroup();
        checkboxGuest = new ColleagueCheckbox("Guest",group,true);
        checkboxLogin = new ColleagueCheckbox("Login",group,false);

       textUser = new ColleagueTextField("", 10);
       textPassword= new ColleagueTextField("", 10);
       textPassword.setEchoChar('*');

       buttonOK  = new ColleagueButton("OK");
       buttonCancel = new ColleagueButton("Cancel");

       checkboxGuest.setMediator(this);
       checkboxLogin.setMediator(this);
       textUser.setMediator(this);
       textPassword.setMediator(this);
       buttonOK.setMediator(this);
       buttonCancel.setMediator(this);
//       设置Listener,java awt和前端联想在一起
       checkboxGuest.addItemListener(checkboxGuest);
       checkboxLogin.addItemListener(checkboxLogin);
       textUser.addTextListener(textUser);
       textPassword.addTextListener(textPassword);
       buttonOK.addActionListener(this);
       buttonCancel.addActionListener(this);

    }

    @Override
    public void colleagueChanged() {
        if(checkboxGuest.getState()){
            textUser.setColleagueEnabled(false);
            textPassword.setColleagueEnabled(false);
            buttonOK.setColleagueEnabled(true);
        }else{
            textUser.setColleagueEnabled(true);
            userPassChanged();
        }
    }

//    密码框改动的逻辑
    private void userPassChanged(){
        if(textUser.getText().length() >= 4){
            textPassword.setColleagueEnabled(true);
            if(textPassword.getText().length() >= 4){
                buttonOK.setColleagueEnabled(true);
            }else{
                buttonOK.setColleagueEnabled(false);
            }
        }else{
            textPassword.setColleagueEnabled(false);
            buttonOK.setColleagueEnabled(false);
        }
    }

    private boolean passwordSecurity(){
        int length = textUser.getText().length();
        int length2 = textPassword.getText().length();
        return length >= 4 && length2 >= 4;
    }
}
